package primary

fun main() {
    val a = 10
    if (a > 10) {
        println("hello!")
    }

    // 1. 基本用法（类似 switch）
    val c = 'A'
    when (c) {
        'A' -> println("love")
        'B' -> println("peace")
        'C' -> println("cat")
        else -> println("unknown")
    }

    // 2. 多个条件匹配
    when (c) {
        'A', 'a' -> println("A or a")
        'B', 'b' -> println("B or b")
        else -> println("not A/B")
    }

    // 3. 范围匹配
    val score = 85
    when (score) {
        in 90..100 -> println("优秀")
        in 80..89 -> println("良好")
        else -> println("其他")
    }

    // 4. 类型检查
    fun checkType(obj: Any) = when (obj) {
        is String -> println("字符串: $obj")
        is Int -> println("整数: $obj")
        else -> println("其他类型")
    }
    checkType("Hello")
    checkType(42)

    // 5. 无参数 when（替代 if-else 链）
    val temperature = 25
    when {
        temperature > 30 -> println("很热")
        temperature in 20..30 -> println("舒适")
        else -> println("冷")
    }
    //for循环
    // 1. 遍历区间
    println("遍历区间 1..5:")
    for (i in 1..5) {
        print("$i ")
    }
    println()

    // 2. 遍历集合
    println("\n遍历列表:")
    val list = listOf("A", "B", "C")
    for (item in list) {
        print("$item ")
    }
    println()

    // 3. 遍历数组
    println("\n遍历数组:")
    val array = arrayOf(1, 2, 3, 4, 5)
    for (num in array) {
        print("$num ")
    }
    println()

    // 4. 使用索引遍历
    println("\n带索引遍历:")
    for ((index, value) in list.withIndex()) {
        println("索引 $index: $value")
    }

    // 5. 反向遍历
    println("\n反向遍历:")
    for (i in 5 downTo 1) {
        print("$i ")
    }
    println()

    // 6. 步长遍历
    println("\n步长为2遍历:")
    for (i in 1..10 step 2) {
        print("$i ")
    }
    println()

    // 7. 遍历 map
    println("\n遍历 map:")
    val map = mapOf("a" to 1, "b" to 2, "c" to 3)
    for ((key, value) in map) {
        println("$key = $value")
    }
    //while循环与其他高级语言一致，也有do{...}while(...)循环
}